package com.glorystone.ins.service.impl;

import java.time.LocalDateTime;
import java.util.*;

import com.glorystone.ins.dao.SysRoleMapper;
import com.glorystone.ins.domain.SysRole;
import com.glorystone.ins.domain.SysUser;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.glorystone.ins.common.common.PageVo;
import com.glorystone.ins.dao.SysAuthMapper;
import com.glorystone.ins.domain.SysAuth;
import com.glorystone.ins.interceptor.PageList;
import com.glorystone.ins.service.ISysAuthService;
import com.glorystone.ins.vo.RootMenu;

/**
 * Created by klaus on 9/27/16.
 */
@Service
public class SysAuthServiceImpl  implements ISysAuthService {
    @Autowired
    private SysAuthMapper sysAuthMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;

    private static final Logger log = LoggerFactory.getLogger(SysAuthServiceImpl.class);

    @Override
    public boolean addAuth(SysAuth auth) {
    	auth.setDelFlag(0);
    	auth.setCreateTime(LocalDateTime.now());
    	auth.setUpdateTime(LocalDateTime.now());
    	int result = sysAuthMapper.insert(auth);
        List<SysAuth> list = sysAuthMapper.getAllAuth();
        String ids = "0,";
        for(SysAuth sah:list){
            ids += sah.getTid()+",";
        }
        SysRole role1 = new SysRole();
        role1.setTid(1L);
        role1.setAuthIds(ids);
        sysRoleMapper.update(role1);
        SysRole role2 = new SysRole();
        role2.setTid(13L);
        role2.setAuthIds(ids);
        sysRoleMapper.update(role2);
        return result >0 ? true : false;
    }

    @Override
    public boolean delAuth(Long authId) {
        SysAuth auth = new SysAuth();
        auth.setTid(authId);
        auth.setDelFlag(0);
    	int result = sysAuthMapper.update(auth);
        return result > 0 ? true : false;
    }

    @Override
    public boolean updateAuth(SysAuth auth) {
    	auth.setUpdateTime(LocalDateTime.now());
    	int result = sysAuthMapper.update(auth);
        return result > 0 ? true : false;
    }

    @Override
    public SysAuth getAuthById(Long authId) {
        return sysAuthMapper.selectByPrimaryKey(authId);
    }



    @Override
    public List<SysAuth> getAllAuth() {
        return sysAuthMapper.getAllAuth();
    }

    @Override
    public List<SysAuth> getRootMenu() {
        return sysAuthMapper.getRootMenu();
    }

    @Override
    public List<SysAuth> getChildMenu() {
        return sysAuthMapper.getChildMenu();
    }


    @Override
    public List<SysAuth> getAuthByRoleId(String roleIds) {
        List<SysAuth> allAuthList = sysAuthMapper.getAllAuth();
        String[] roleIdArray = roleIds.split(",");
        String authIds = "";
        for(int i=0;i<roleIdArray.length;i++){
            String roleId = roleIdArray[i].trim();
            if("0".equals(roleId)){
                continue;
            }
            if("13".equals(roleId)||"1".equals(roleId)){//管理员拥有所有菜单权限
                return allAuthList;
            }
            SysRole role = sysRoleMapper.selectByPrimaryKey(Long.parseLong(roleId));
            authIds += role.getAuthIds();
        }
        String[] authIdArray = authIds.split(",");
        Set<String> authIdSet = new HashSet<>();
        for(int i=0;i<authIdArray.length;i++){
            authIdSet.add(authIdArray[i]);
        }
        String newAuthIds = "0,";
        Iterator it = authIdSet.iterator();
        while(it.hasNext()){
            newAuthIds += it.next()+",";
        }
        List<SysAuth> roleAuthList = Lists.newArrayList();
        for(SysAuth sysAuth:allAuthList){
            if(newAuthIds.indexOf(","+sysAuth.getTid()+",")>-1){
                roleAuthList.add(sysAuth);
            }
        }
        return roleAuthList;
    }

    @Override
    public PageList<SysAuth> queryAuthList(PageVo vo) {
        return sysAuthMapper.findAuthList(vo, vo.getPageable());
    }

    /**
     * 获取所有主菜单页面
     *//*
    @Override
    public List<SysAuth> getRootMenu() {
        List<SysAuth> list = sysAuthMapper.getRootMenu();
        SysAuth sa = new SysAuth();
        sa.setId(0);
        sa.setText("根级菜单");
        list.add(sa);
        return list;
    }

    *//**
     * 获取子菜单集合
     *//*
    @Override
    public List<SysAuth> getChildMenu() {
        return sysAuthMapper.getChildMenu();
    }

    *//**
     * 通过roleId获取主菜单页面
     *
     * @param roleId
     *//*
    @Override
    public List<SysAuth> getRootMenuByRoleId( int roleId) {
         
        return new ArrayList<>() ;
    }

    *//**
     * 通过roleId获取子菜单页面
     *
     * @param roleId
     *//*
    @Override
    public List<SysAuth> getChildMenuByRoleId( int roleId) {
        return new ArrayList<>();
    }
 

    @Override
    public PageList<SysAuth> queryAuthList(PageVo vo) {
        return sysAuthMapper.findAuthList(vo, vo.getPageable());
    }

    @Override
    public List<RootMenu> getRootMenuList(Integer id) {

        List<RootMenu>  list = new ArrayList<>();
        List<SysAuth>  rootMenus = new ArrayList<>();
        if(id == null){
        	rootMenus = sysAuthMapper.getRootMenu();//顶级菜单
        }else{
        	rootMenus = sysAuthMapper.getRootMenuForRole(id);//顶级菜单
        }
        for(SysAuth sysAuth:rootMenus){
//        	sysAuth.setRoleId(id);
            List<RootMenu.ChildMenu> childMenus = new ArrayList<>();
            List<SysAuth> childMenuList  = sysAuthMapper.selectChildAuthListByParentId1(sysAuth);
            for(SysAuth sa:childMenuList){
//            	sa.setRoleId(id);
                List<SysAuth> childButtonLit = sysAuthMapper.selectChildAuthListByParentId1(sa);
                RootMenu.ChildMenu childMenu = new RootMenu.ChildMenu();
                childMenu.setChildMenuSysAuth(sa);
                childMenu.setChildButtons(childButtonLit);
                childMenus.add(childMenu);
            }
            RootMenu rm = new RootMenu();
            rm.setRootMenuSysAuth(sysAuth);
            rm.setChildMenus(childMenus);
            list.add(rm);
        }
        return list;
    }

    *//**
     * 修改
     * @param roleId
     * @param authIdList
     * @return
     *//*
    @Override
    @Transactional
    public boolean editTreeMenuByRoleId(int roleId, List<Integer> authIdList) {
        //删除roleId对应的authId
        try {
            sysAuthMapper.deleteRoleAuthByRoleId(roleId);
            //插入新的roleId与authId关系中间表
            Map<String,Object> params = new HashMap<String, Object>();
            params.put("roleId", roleId);
            params.put("authIds", authIdList);

            return true;
        }catch (Exception e){
            e.printStackTrace();
            log.info("数据库异常");
            return false;
        }
    }*/
}
